package com.boarsoft.boar.batch.demo.flow.bigfile;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.boarsoft.boar.batch.demo.biz.Coredpfm02Biz;
import com.boarsoft.boar.batch.demo.entity.Coredpfm02;
import com.boarsoft.boar.batch.demo.util.ByteString;
import com.boarsoft.boar.batch.demo.util.ToBigDecimal;
import com.boarsoft.flow.core.bean.ProcessHandler;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

/**
 * @ClassName:  LoadDpfm01BatchSave   
 * @Description:被fork出的线程，每个线程里面批量save
 * @author: 
 * @date:   2019年2月15日 上午9:50:59   
 * @Copyright: 2019
 */
public class LoadDpfm02BatchSave extends SimpleBizImpl implements ProcessHandler {
	private static final Logger log = LoggerFactory.getLogger(LoadDpfm02BatchSave.class);

	@Autowired
	protected Coredpfm02Biz biz;

	@Override
	public Object process(String entry, Object data, Throwable throwable) throws Throwable {
		try {
			Map<String, Object> pm = (Map<String, Object>) data;
			final int MAX_COMMIT = (int) pm.get("commitCount");
			final String fileName = (String) pm.get("fileName");
			RandomAccessFile aFile = new RandomAccessFile(fileName, "r");
			FileChannel inChannel = aFile.getChannel();
			ByteBuffer buf = ByteBuffer.allocate(464);
			String[] stringField = new String[64];
			List<Coredpfm02> list0 = new ArrayList<>();
			List<Coredpfm02> list1 = new ArrayList<>();
			int i = 1;
			while (inChannel.read(buf) != -1) {
				buf.flip();
				String string = ByteString.getString(buf);

				stringField = string.split("\03");

				Coredpfm02 dpfm = new Coredpfm02();

				dpfm.setDM02ACCT(stringField[0]);
				dpfm.setDM02ACCS(stringField[1]);
				dpfm.setDM02ACCTYP(stringField[2]);
				dpfm.setDM02HDFLG(stringField[3]);
				dpfm.setDM02CLSFLG(stringField[4]);
				dpfm.setDM02CCY(stringField[5]);
				dpfm.setDM02CSHTRF(stringField[6]);
				dpfm.setDM02PRDID(stringField[7]);
				dpfm.setDM02SBNO(stringField[8]);
				dpfm.setDM02OPNDAT(stringField[9]);
				dpfm.setDM02STRDAT(stringField[10]);
				dpfm.setDM02ENDDAT(stringField[11]);
				dpfm.setDM02TERM1(ToBigDecimal.stringToBigDecimal(stringField[12]));
				dpfm.setDM02TERM2(stringField[13]);
				dpfm.setDM02BALE(ToBigDecimal.stringToBigDecimal(stringField[14]));
				dpfm.setDM02BALD(stringField[15]);
				dpfm.setDM02BALEN(ToBigDecimal.stringToBigDecimal(stringField[16]));
				dpfm.setDM02EBALD(stringField[17]);
				dpfm.setDM02LBALE(ToBigDecimal.stringToBigDecimal(stringField[18]));
				dpfm.setDM02LBALD(stringField[19]);
				dpfm.setDM02CTLBAL(ToBigDecimal.stringToBigDecimal(stringField[20]));
				dpfm.setDM02FRZBAL(ToBigDecimal.stringToBigDecimal(stringField[21]));
				dpfm.setDM02OVDBAL(ToBigDecimal.stringToBigDecimal(stringField[22]));
				dpfm.setDM02CVDB1(ToBigDecimal.stringToBigDecimal(stringField[23]));
				dpfm.setDM02CVDB2(ToBigDecimal.stringToBigDecimal(stringField[24]));
				dpfm.setDM02CVDB3(ToBigDecimal.stringToBigDecimal(stringField[25]));
				dpfm.setDM02ITPLAN(stringField[26]);
				dpfm.setDM02ITFLT(ToBigDecimal.stringToBigDecimal(stringField[27]));
				dpfm.setDM02LSTDTE(stringField[28]);
				dpfm.setDM02NODE(stringField[29]);
				dpfm.setDM02ACCCHR(stringField[30]);
				dpfm.setDM02ACCCH1(stringField[31]);
				dpfm.setDM02YZFLG(stringField[32]);
				dpfm.setDM02TDUCTL(stringField[33]);
				dpfm.setDM02CSHFLG(stringField[34]);
				dpfm.setDM02ADVTIM(ToBigDecimal.stringToBigDecimal(stringField[35]));
				dpfm.setDM02DMOD(stringField[36]);
				dpfm.setDM02INTRAT(ToBigDecimal.stringToBigDecimal(stringField[37]));
				dpfm.setDM02FRZFLG(stringField[38]);
				dpfm.setDM02MRTFLG(stringField[39]);
				dpfm.setDM02PRVFLG(stringField[40]);
				dpfm.setDM02PBLIN(ToBigDecimal.stringToBigDecimal(stringField[41]));
				dpfm.setDM02CVNFLG(stringField[42]);
				dpfm.setDM02SECFLG(stringField[43]);
				dpfm.setDM02OVDFLG(stringField[44]);
				dpfm.setDM02IINFLG(ToBigDecimal.stringToLong(stringField[45]));
				dpfm.setDM02INTFLG(ToBigDecimal.stringToLong(stringField[46]));
				dpfm.setDM02INTFLG1(ToBigDecimal.stringToLong(stringField[47]));
				dpfm.setDM02NRAMT(ToBigDecimal.stringToBigDecimal(stringField[47]));
				dpfm.setDM02PMSAMT(ToBigDecimal.stringToBigDecimal(stringField[49]));
				dpfm.setDM02INTPRD(ToBigDecimal.stringToBigDecimal(stringField[50]));
				dpfm.setDM02DONTIM(ToBigDecimal.stringToBigDecimal(stringField[51]));
				dpfm.setDM02NXDAT(stringField[52]);
				dpfm.setDM02LZFLG(stringField[53]);
				dpfm.setDM02LIEID(ToBigDecimal.stringToBigDecimal(stringField[54]));
				dpfm.setDM02DTFLG(stringField[55]);
				dpfm.setDM02ZCTIM(ToBigDecimal.stringToBigDecimal(stringField[56]));
				dpfm.setDM02CONNO(stringField[57]);
				dpfm.setDM02FEEFLG(stringField[58]);
				dpfm.setDM02REMARK(stringField[59]);
				dpfm.setDM02OLBAL(ToBigDecimal.stringToBigDecimal(stringField[60]));
				dpfm.setDM02FLAG(stringField[61]);
				dpfm.setDEL_F(ToBigDecimal.stringToLong(stringField[62]));
				dpfm.setLAST_ETL_ACG_DT(stringField[63]);
				
				if (getDataSource(Integer.valueOf(dpfm.getDM02CLSFLG())) == 0) {
					list0.add(dpfm);
				} else {
					list1.add(dpfm);
				}
				
				if (i % MAX_COMMIT == 0) {
					System.out.println("每"+ MAX_COMMIT+ "行提交一次  " + i);
					biz.batchSave(0, list0);
					biz.batchSave(1, list1);
					list0.clear();
					list1.clear();
				}
				
				i++;

				stringField = null;
				string = null;
				buf.clear();
			}

			System.out.println("最后提交一次 i = " + i + "最后一次行数：" + i % MAX_COMMIT);
			biz.batchSave(0, list0);
			biz.batchSave(1, list1);
			list0.clear();
			list1.clear();
			aFile.close();

			System.out.println("本线程装载完成: " + Thread.currentThread().getId() + "    "+ "处理的文件名称：" + fileName);
			System.out.println("本线程耗时:" + (System.currentTimeMillis() - LoadDpfm02Fork.loadDpfm02ForkStart) + "ms.");
		} catch (IOException e) {
			log.info(e.getMessage());
		}
		
		return data;
	}
	
	public int getDataSource(int tenantId) {
		return tenantId % 2;
	}
}
